//
//  MYGPhoneBindController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/11.
//

import UIKit

class MYGPhoneBindController: MYGBaseController {
    // MARK: - init
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        modalPresentationStyle = .custom
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .black.withAlphaComponent(0.6)
        view.addSubview(mainView)
        mainView.addSubview(nameLabel)
        mainView.addSubview(phoneTextField)
        mainView.addSubview(codeTextField)
        mainView.addSubview(lineView)
        mainView.addSubview(currentButton)
        
        codeTextField.rightView?.addSubview(codeButton)
        codeButton.center = codeTextField.rightView?.center ?? .zero
        
        mainView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(315)
            make.height.equalTo(240)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(24)
        }
        
        phoneTextField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(18)
            make.bottom.equalTo(codeTextField.snp.top).offset(-15)
            make.height.equalTo(40)
        }
        
        codeTextField.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(18)
            make.bottom.equalTo(lineView.snp.top).offset(-18)
            make.height.equalTo(40)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(currentButton.snp.top)
            make.height.equalTo(0.5)
        }
        
        currentButton.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(52)
        }
    }
    
    // MARK: - lazy load
    private lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 12
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(17)
        label.textColor = .rgba(54, 54, 54, 1)
        label.text = "绑定手机号"
        return label
    }()
    
    private lazy var phoneTextField: UITextField = {
        let tf = UITextField()
        
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 2.5
        tf.layer.borderColor = UIColor.rgba(238, 238, 238, 1).cgColor
        tf.layer.borderWidth = 1
        
        tf.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 14, height: 40))
        tf.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 14, height: 40))
        tf.leftViewMode = .always
        tf.rightViewMode = .always
        tf.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
            confer.text("请输入手机号")?.font(.regular(15)).textColor(.rgba(148, 148, 148, 1))
        })
        
        tf.font = .regular(15)
        tf.textColor = .rgba(51, 51, 51)
        
        tf.keyboardType = .phonePad
        return tf
    }()
    
    private lazy var codeTextField: UITextField = {
        let tf = UITextField()
        
        tf.layer.masksToBounds = true
        tf.layer.cornerRadius = 2.5
        tf.layer.borderColor = UIColor.rgba(238, 238, 238, 1).cgColor
        tf.layer.borderWidth = 1
        
        tf.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 14, height: 40))
        tf.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 97, height: 40))
        tf.leftViewMode = .always
        tf.rightViewMode = .always
        
        tf.attributedPlaceholder = NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
            confer.text("请输入验证码")?.font(.regular(15)).textColor(.rgba(148, 148, 148, 1))
        })
        
        tf.font = .regular(15)
        tf.textColor = .rgba(51, 51, 51)
        
        tf.keyboardType = .numberPad
        return tf
    }()
    
    private lazy var codeButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("发送验证码", for: .normal)
        btn.setTitleColor(.rgba(69, 131, 250, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.sizeToFit()
        return btn
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(212, 212, 212, 1)
        return view
    }()
    
    private lazy var currentButton: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(.rgba(69, 131, 250, 1), for: .normal)
        btn.setTitle("确定", for: .normal)
        btn.titleLabel?.font = .regular(17)
        return btn
    }()
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss(animated: false)
    }
}
